Application.Exit() :
The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread. When calling Application.Exit everything is told to stop immediately and then the application is shutdown. Resulting in events like Form.Closed and Form.Closing not are being fired.
Exit raises the following events and performs the associated conditional actions:
· A FormClosing event is raised for every form represented by the OpenForms property. This event can be canceled by setting the Cancel property of their FormClosingEventArgs parameter to true.
· If one of more of the handlers cancels the event, then Exit returns without further action. Otherwise, a FormClosed event is raised for every open form, and then all running message loops and forms are closed.
this.Close() :
this.Close() are generally use for close the form, On respect of database connection this.Close() leaves the connection in a closed state but, it is reusable—all properties, etc. because form are not actually removed from memory. this.Close() will call Form.Close method of current form. If it's the startup main form, application will exit automatically after main form closing, otherwise, it just close the current form. That is when a Close () method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used
this.close() are used when we want to reuse the form property or form object then there we will not use this.Dispose().
this.Dispose() :
this.Dispose() are used for permanently removed form or connection from memeory. Dispose () method permanently removes any resource (managed or unmanaged) from memory for cleanup and the resource no longer exists for any further processing. It is basically used when we do not reuse resources.
namespace MDIApplication
{
public partial class Form1 : Form
{
NewForm newForm;
// MdI form Constructor
public Form1()
{
InitializeComponent();
CenterToScreen();
}
// Select this item from menu item bar
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
newForm = new NewForm();
newForm.MdiParent = this;
newForm.Show();
}
// Select Exit item from menu item bar
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
// Dispose Methode Call
this.Dispose();
}
// Select close item from menu item bar
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
// check activated form
Form activeForm = NewForm.ActiveForm;
if (activeForm != null)
{
// Close activated form
newForm.Close();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
// Exit Methode Call when click application exit menu item
Application.Exit();
}
}
}
Anonymous User
30-Jan-2019asdf asd
28-Nov-2017when i call myForm.Dispose() it closes the entire application, not only the myForm.
I have this in myForm :
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Samuel Fernandes
07-Aug-2017It is great to associate with such a blog.